package org.exteen.tag.core;

import org.exteen.ExteenException;
import org.exteen.exec.DynamicContext;
import org.exteen.exec.ExteenContext;
import org.exteen.exec.Tag;
import org.exteen.output.ExteenStream;
import org.exteen.parser.Node;

public class InvokeBodyTag extends Tag {

    public void doTag(ExteenContext ctx, ExteenStream stream) {
        Node invokenode = ctx.getNode();
        DynamicContext dctx = (DynamicContext) ctx.getParentOfType(DynamicContext.class);
        if (dctx == null) {
            throw new ExteenException("No body to invoke");
        }

        String bodyTagName = null;
        Object bodyTagNameObj = invokenode.getAttributeValue("tag", ctx);
        if (bodyTagNameObj != null) {
            bodyTagName = bodyTagNameObj.toString();
        }

        Node callerBody = dctx.getCallerBody(bodyTagName);
        ExteenContext ctxAroundDynamicTag = dctx.getParent();
        ExteenContext chldCtx = ctxAroundDynamicTag.createChildContext(callerBody);
        super.doBody(chldCtx, stream);
        chldCtx.release();
    }
}